<template>
  <div class="product-list-wrap">
    <div class="product-list-content">
      <header class="category-header wrap">
        <i class="iconfont icon-fanhui1" @click="goBack"></i>
        <div class="header-search">
          <i class="iconfont icon-sousuo"></i>
          <input type="text" class="search-title" v-model="keyword" />
        </div>
        <span class="search-btn" @click="getSearch">搜索</span>
      </header>
      <van-tabs type="card" color="#1baeae" @click="changeTab">
        <van-tab title="推荐" name="recommend" :sort ='sort'>
            <van-pull-refresh class="product-list-refresh" v-model="refreshing" @refresh="onRefresh">
                <van-list v-model="loading" :finished="finished" finished-text="没有更多了" @load="onLoad" @offset="300">
                    <div class="product-item" v-for="(item, index) in productList" :key="index" @click="productDetail(item)">
                        <img :src="item.image"  alt="图片加载中"/>
                        <div class="product-info">
                            <p class="name">{{ item.goods_name }}</p>
                            <p class="subtitle">   {{ item.introduce }}</p>
                            <span class="price">￥ {{item.price}}</span>
                        </div>
                    </div>
                </van-list>
            </van-pull-refresh>
        </van-tab>
        <van-tab title="新品" name="new" :sort ='sort'>
          <van-pull-refresh class="product-list-refresh" v-model="refreshing" @refresh="onRefresh">
                <van-list v-model="loading" :finished="finished" finished-text="没有更多了" @load="onLoad" @offset="300">
                    <div class="product-item" v-for="(item, index) in productList" :key="index" @click="productDetail(item)">
                        <img :src="item.image"  alt="图片加载中"/>
                        <div class="product-info">
                            <p class="name">{{ item.goods_name }}</p>
                            <p class="subtitle">   {{ item.introduce }}</p>
                            <span class="price">￥ {{item.price}}</span>
                        </div>
                    </div>
                </van-list>
            </van-pull-refresh>
        </van-tab>
        <van-tab title="价格" name="price" :sort ='sort'>
          <van-pull-refresh class="product-list-refresh" v-model="refreshing" @refresh="onRefresh">
                <van-list v-model="loading" :finished="finished" finished-text="没有更多了" @load="onLoad" @offset="300">
                    <div class="product-item" v-for="(item, index) in productList" :key="index" @click="productDetail(item)">
                        <img :src="item.image"  alt="图片加载中"/>
                        <div class="product-info">
                            <p class="name">{{ item.goods_name }}</p>
                            <p class="subtitle">   {{ item.introduce }}</p>
                            <span class="price">￥ {{item.price}}</span>
                        </div>
                    </div>
                </van-list>
            </van-pull-refresh>
        </van-tab>
      </van-tabs>
    </div>
  </div>
</template>
<script setup lang="ts">
import { onMounted, ref } from 'vue'
import { useRouter ,useRoute} from 'vue-router'
import { search } from '../service/good'
interface ProductListItem {
    goods_id: number,
    goods_name: string,
    introduce: string,
    image: string,
    price: number
}
const sort =  ref<number>(1)
const router = useRouter()
const route = useRoute()
const refreshing = ref<boolean>(false)
const loading = ref<boolean>(false)
const finished = ref<boolean>(false)
const productList = ref<ProductListItem []>([])
const page = ref<number>(1)
const totalPage = ref<number>(0)
const categoryId = ref<number>()
const keyword = ref<string>('' )
const orderBy = ref('')
// 初始化列表数据
async function init () {
  const {Id,from} = route.query
  if(!Id && keyword.value){
    finished.value = true
    loading.value = false
  }
  const {
    data,
    data: { list }
  } = await search(
    {
    "category": "",
    "name": keyword.value,
    "page": page.value,
    "sort": sort.value
}
  )
  productList.value = productList.value.concat(list)
  totalPage.value = data.total_page
  loading.value = false
  // console.log(productList.value)
  totalPage.value = data.totalPage
  loading.value = true

  if (page.value > totalPage.value) {
    finished.value = true
  }
}

function goBack () {
  router.go(-1)
}
function productDetail (item:any) {
  router.push({ path: `product/${item.goods_id}` })
}
function onLoad () {
  if (!refreshing.value && page.value <= totalPage.value) {
    page.value = page.value + 1;
    // 请求数据
  console.log("onload",page.value);

  }
    // page.value = page.value + 1

  // console.log("onload",page.value);
  if (refreshing.value) {
    productList.value = []
    refreshing.value = false
  }
  init()
}
function onRefresh () {
  refreshing.value = false
  loading.value = true
  finished.value = false
  page.value = 1
  console.log("@onRefresh");
  
  onLoad()
}
function getSearch () {
  onRefresh()
}
function changeTab (name :string, title :string) {
  orderBy.value = name
  onRefresh()
  if(name === 'recommend') {
    sort.value = 2
  }else if(name ==='new') {
    sort.value = 1
  } else {
    sort.value =3
  }
  console.log(sort.value);
  
}
onMounted(() => {
  init()
})
</script>

<style lang="less" scoped>
  @import "../common/style/mixin.less";
  
  .product-list-content {
    position: fixed;
    left: 0;
    top: 0;
    width: 100%;
    z-index: 1000;
    background: #fff;
  
    .category-header {
      .fj();
      width: 100%;
      height: 50px;
      line-height: 50px;
      padding: 0 15px;
      .boxSizing();
      font-size: 15px;
      color: #656771;
      z-index: 10000;
  
      &.active {
        background: @primary;
      }
  
      .icon-left {
        font-size: 25px;
        font-weight: bold;
      }
  
      .header-search {
        display: flex;
        width: 76%;
        height: 20px;
        line-height: 20px;
        margin: 10px 0;
        padding: 5px 0;
        color: #232326;
        background: #f7f7f7;
        .borderRadius(20px);
  
        .icon-icon-search {
          padding: 0 5px 0 20px;
          font-size: 17px;
        }
  
        .search-title {
          font-size: 12px;
          color: #666;
          background: #f7f7f7;
        }
      }
  
      .icon-More {
        font-size: 20px;
      }
  
      .search-btn {
        height: 28px;
        margin: 8px 0;
        line-height: 28px;
        padding: 0 5px;
        color: #fff;
        background: @primary;
        .borderRadius(5px);
        margin-top: 10px;
      }
    }
  }
  
  .product-list-refresh {
    .product-item {
      .fj();
      width: 100%;
      height: 120px;
      padding: 10px;
      border-bottom: 1px solid #dcdcdc;
  
      img {
        width: 140px;
        height: 120px;
        padding: 0 10px;
        .boxSizing();
      }
  
      .product-info {
        width: 56%;
        height: 120px;
        padding: 5px;
        text-align: left;
        .boxSizing();
  
        p {
          margin: 0;
        }
  
        .name {
          width: 100%;
          max-height: 40px;
          line-height: 20px;
          font-size: 15px;
          color: #333;
          overflow: hidden;
          text-overflow: ellipsis;
          white-space: nowrap;
        }
  
        .subtitle {
          width: 100%;
          max-height: 20px;
          padding: 10px 0;
          line-height: 25px;
          font-size: 13px;
          color: #999;
          overflow: hidden;
        }
  
        .price {
          color: @primary;
          font-size: 16px;
        }
      }
    }
    .van-list{
      height: 560px;
      overflow-y: scroll;
    }
  }
  </style>